package com.nowcoder.Offer.String;

import org.junit.Test;

/**
 * 将一个字符串转换成一个整数
 */
public class StrToInt {



    public int StrToInt(String str) {
        char[] c = str.trim().toCharArray();
        if(c.length == 0) return 0;
        int res = 0, outVal = Integer.MAX_VALUE/10;
        int i = 1, sign = 1;
        if(c[0] == '-') sign = -1;
        else if(c[0] !='+') i = 0;
        for(int j=i; j<str.length(); j++){
            if(c[j] >'9' || c[j] <'0')  break;
            if(res > outVal || res == outVal && c[j] >'7') return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            res = res * 10 + (c[j] -'0');
        }
        return sign * res;
    }

    @Test
    public void test(){

    }
}
